using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Common;
using MinCultura.Sipa.DataAccess.AgentesArtes;
using SystemFrameworks;

namespace MinCultura.Sipa.AgentesArtes
{
	/// <summary>
	/// Clase de acceso a datos para la entidad ART_AGENTES_AREAS_TEMAS
	/// </summary>
	public class AgentesAreasTemas 
	{
		#region Private Data Access Atributes
		private const String AgeIdParam = "@AGE_ID";
		private const String AatIdParam = "@AAT_ID";
		#endregion Private Data Access Atributes

        #region propiedades

        private string _cadenaConexion;

        public string ConnectionString
        {
            get { return _cadenaConexion; }
            set { _cadenaConexion = value; }
        }

        #endregion

		/// <summary>
		/// Constructor de la clase ARTAGENTESAREASTEMAS
		/// </summary>
        public AgentesAreasTemas(string strCadenaConecxion)
        {
            ConnectionString = strCadenaConecxion;
        }		

		public AgentesAreasTemasData BuscarAgentesAreasTemas()
		{
			AgentesAreasTemasData response = new AgentesAreasTemasData();
			try
			{
				SqlHelper.ExecuteDataTable(ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_AGENTES_AREAS_TEMAS", response);
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_BUSCAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorBuscar", GetType().FullName)+ ".<br>" + ex.Message, ex);
			}
			if (response.Rows.Count == 0)
				return null;
			return response;
		}


		public AgentesAreasTemasData ConsultarAgentesAreasTemas(decimal parAgeId, decimal parAatId)
		{
			AgentesAreasTemasData response = new AgentesAreasTemasData();
			SqlParameter param1 = new SqlParameter(AgeIdParam, parAgeId);
			SqlParameter param2 = new SqlParameter(AatIdParam, parAatId);
			SqlParameter[] parameters = {param1, param2};
			try
			{
				SqlHelper.ExecuteDataTable(ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AGENTES_AREAS_TEMAS", response, parameters);
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_CONSULTAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorConsultar", GetType().FullName)+ ".<br>" + ex.Message, ex);
			}
			if (response.Rows.Count == 0)
				return null;
			return response;
		}

		public AgentesAreasTemasData ConsultarAgentesAreasTemas(decimal parAgeId)
		{
			AgentesAreasTemasData response = new AgentesAreasTemasData();
			SqlParameter param1 = new SqlParameter(AgeIdParam, parAgeId);
			SqlParameter[] parameters = {param1};
			try
			{
				SqlHelper.ExecuteDataTable(ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AGENTES_AREAS_TEMAS", response, parameters);
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_CONSULTAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorConsultar", GetType().FullName)+ ".<br>" + ex.Message, ex);
			}
			if (response.Rows.Count == 0)
				return null;
			return response;
		}

		public void AdicionarAgentesAreasTemas(AgentesAreasTemasData datos)
		{					
			foreach (DataRow row in datos.Rows)
			{
				SqlParameter param1 = new SqlParameter(AgeIdParam, row[AgentesAreasTemasData.AGE_ID_FIELD]);
				SqlParameter param2 = new SqlParameter(AatIdParam, row[AgentesAreasTemasData.AAT_ID_FIELD]);
				SqlParameter[] parameters = {param1, param2};
				try
				{
					SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AGENTES_AREAS_TEMAS", parameters);
				}
				catch (SqlException sqlEx)
				{
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ADICIONAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
				}
				catch (Exception ex)
				{
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", GetType().FullName)+ ".<br>" + ex.Message, ex);
				}
			}		
		}

		public bool Adicionar(AgentesAreasTemasData datos,params SqlTransaction[] transaction)
		{		
			bool response=false;
			foreach (DataRow row in datos.Rows)
			{
				SqlParameter param1 = new SqlParameter(AgeIdParam, row[AgentesAreasTemasData.AGE_ID_FIELD]);
				SqlParameter param2 = new SqlParameter(AatIdParam, row[AgentesAreasTemasData.AAT_ID_FIELD]);
				SqlParameter[] parameters = {param1, param2};
				try
				{
					if (transaction.Length>0)
						SqlHelper.ExecuteNonQuery(transaction[0], CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AGENTES_AREAS_TEMAS", parameters);
					else
						SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AGENTES_AREAS_TEMAS", parameters);
				    response = true;
				}
				catch (SqlException sqlEx)
				{
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ADICIONAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
				}
				catch (Exception ex)
				{
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", GetType().FullName)+ ".<br>" + ex.Message, ex);
				}
			}
			return response;
		}

		public bool Eliminar(AgentesAreasTemasData datos,params SqlTransaction[] transaction)
		{		
			bool response = false;
			foreach (DataRow row in datos.Rows)
			{
				SqlParameter param1 = new SqlParameter(AgeIdParam, row[AgentesAreasTemasData.AGE_ID_FIELD]);
				SqlParameter param2 = new SqlParameter(AatIdParam, row[AgentesAreasTemasData.AAT_ID_FIELD]);
				SqlParameter[] parameters = {param1, param2};
				try
				{
					if (transaction.Length>0)
						SqlHelper.ExecuteNonQuery(transaction[0], CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);
					else
						SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);
                    response = true;
				}
				catch (SqlException sqlEx)
				{
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
				}
				catch (Exception ex)
				{
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", GetType().FullName)+ ".<br>" + ex.Message, ex);
				}
			}
			return response;
		}

		public void ActualizarAgentesAreasTemas(AgentesAreasTemasData datos)
		{			
			foreach (DataRow row in datos.Rows)
			{
				SqlParameter param1 = new SqlParameter(AgeIdParam, row[AgentesAreasTemasData.AGE_ID_FIELD]);
				SqlParameter param2 = new SqlParameter(AatIdParam, row[AgentesAreasTemasData.AAT_ID_FIELD]);
				SqlParameter[] parameters = {param1, param2};
				try
				{
					SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PAS_ACTUALIZAR_ART_AGENTES_AREAS_TEMAS", parameters);
				}
				catch (SqlException sqlEx)
				{
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ACTUALIZAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
				}
				catch (Exception ex)
				{
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", GetType().FullName)+ ".<br>" + ex.Message, ex);
				}
			}
		}

		/// <summary>
		/// Elimina un registro de la entidad Artagentesareastemas
		/// </summary>
		/// <param name="parAatId"/>
		/// <param name="parAgeId"/>		
		public void EliminarAgentesAreasTemas(decimal parAgeId, decimal parAatId)
		{
			SqlParameter param1 = new SqlParameter(AgeIdParam, parAgeId);
			SqlParameter param2 = new SqlParameter(AatIdParam, parAatId);
			SqlParameter[] parameters = {param1, param2};
			try
			{				
				SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", GetType().FullName)+ ".<br>" + ex.Message, ex);
			}
		}


		public void EliminarAgentesAreasTemas(decimal parAgeId,params SqlTransaction[] transaction)
		{
			SqlParameter param1 = new SqlParameter(AgeIdParam, parAgeId);
			SqlParameter[] parameters = {param1};
			try
			{				
				if (transaction.Length>0)
					SqlHelper.ExecuteNonQuery(transaction[0], CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);
				else
                    SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", GetType().FullName)+ ".<br>" + ex.Message, ex);
			}
		}
		public bool Eliminar(decimal parAgeId,params SqlTransaction[] transaction)
		{			
			SqlParameter param1 = new SqlParameter(AgeIdParam, parAgeId);
			SqlParameter[] parameters = {param1};
			try
			{				
				if (transaction.Length>0)
					SqlHelper.ExecuteNonQuery(transaction[0], CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);
				else				
					SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);								
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", GetType().FullName)+ ".<br>" + ex.Message, ex);
			}			
			return true;
		}

		public void EliminarAgentesAreasTemas(ArrayList arrIDs)
		{
			SqlConnection objConnection = new SqlConnection(ConnectionString);
			objConnection.Open();
			SqlTransaction objTrans = objConnection.BeginTransaction();

			foreach (object item in arrIDs)
			{
				SqlParameter param1 = new SqlParameter(AgeIdParam, decimal.Parse(item.ToString()));
				SqlParameter param2 = new SqlParameter(AatIdParam, decimal.Parse(item.ToString()));
				SqlParameter[] parameters = {param1, param2};
				try
				{				
					SqlHelper.ExecuteNonQuery(objTrans, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS", parameters);				
				}
				catch (SqlException sqlEx)
				{
					objTrans.Rollback();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ELIMINAR_ART_AGENTES_AREAS_TEMAS"), sqlEx);
				}
				catch (Exception ex)
				{
					objTrans.Rollback();
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", GetType().FullName)+ ".<br>" + ex.Message, ex);
				}
			}
			objTrans.Commit();
			objConnection.Close();			
		}
		
		public bool EjecutarCambios(AgentesAreasTemasData datos, SqlTransaction itransac)
		{
			//mensaje = String.Empty;
			bool response = false;
			bool responseAdd = false;
			bool responseDelete = false;
			AgentesAreasTemasData dataAdicionar = new AgentesAreasTemasData();
			AgentesAreasTemasData dataEliminar = new AgentesAreasTemasData();

			if (datos != null && datos.Rows.Count > 0)
			{
				foreach (DataRow row in datos.Rows)
				{
					switch (row.RowState)
					{
						case DataRowState.Added:
							dataAdicionar.ImportRow(row);
							break;
						case DataRowState.Deleted:
							DataRow dataRow = dataEliminar.NewRow();
							dataRow[AgentesAreasTemasData.AGE_ID_FIELD] = row[AgentesAreasTemasData.AGE_ID_FIELD, DataRowVersion.Original];
							dataRow[AgentesAreasTemasData.AAT_ID_FIELD] = row[AgentesAreasTemasData.AAT_ID_FIELD, DataRowVersion.Original];							
							dataEliminar.Rows.Add(dataRow);
							break;
					}
				}
			}
			try
			{
				if (dataAdicionar != null && dataAdicionar.Rows.Count > 0)
				{
					responseAdd = Adicionar(dataAdicionar, itransac);					
				}
				if (dataEliminar != null && dataEliminar.Rows.Count > 0)
				{
					responseDelete = Eliminar(dataEliminar, itransac);
				}				
			}
			catch (Exception ex)
			{				
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgCambiosNoExitoso", GetType().FullName), ex);
			}			
			if (responseAdd || responseDelete)
			{
				response = true;
			}			
			return response;
		}
	}
}